// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// ███╗   ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗  ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║   ██║   ██║   ██║██║     █████╗
// ██║╚██╗██║██║   ██║   ██║   ██║██║     ██╔══╝
// ██║ ╚████║╚██████╔╝   ██║   ██║╚██████╗███████╗
// ╚═╝  ╚═══╝ ╚═════╝    ╚═╝   ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

#nullable restore
namespace Elastic.Clients.Elasticsearch
{
	public sealed partial class Source
	{
		[JsonInclude]
		[JsonPropertyName("_source")]
		public Elastic.Clients.Elasticsearch.Fields? SourceFields { get; set; }

		[JsonInclude]
		[JsonPropertyName("index")]
		public Elastic.Clients.Elasticsearch.Indices Index { get; set; }

		[JsonInclude]
		[JsonPropertyName("query")]
		public Elastic.Clients.Elasticsearch.QueryDsl.QueryContainer? Query { get; set; }

		[JsonInclude]
		[JsonPropertyName("remote")]
		public Elastic.Clients.Elasticsearch.RemoteSource? Remote { get; set; }

		[JsonInclude]
		[JsonPropertyName("runtime_mappings")]
		public Dictionary<Elastic.Clients.Elasticsearch.Field, IEnumerable<Elastic.Clients.Elasticsearch.Mapping.RuntimeField>>? RuntimeMappings { get; set; }

		[JsonInclude]
		[JsonPropertyName("size")]
		public int? Size { get; set; }

		[JsonInclude]
		[JsonPropertyName("slice")]
		public Elastic.Clients.Elasticsearch.SlicedScroll? Slice { get; set; }

		[JsonInclude]
		[JsonPropertyName("sort")]
		[JsonConverter(typeof(SortConverter))]
		public IEnumerable<Elastic.Clients.Elasticsearch.SortCombinations>? Sort { get; set; }
	}

	public sealed partial class SourceDescriptor<TDocument> : SerializableDescriptorBase<SourceDescriptor<TDocument>>
	{
		internal SourceDescriptor(Action<SourceDescriptor<TDocument>> configure) => configure.Invoke(this);
		public SourceDescriptor() : base()
		{
		}

		private Elastic.Clients.Elasticsearch.QueryDsl.QueryContainer? QueryValue { get; set; }

		private QueryDsl.QueryContainerDescriptor<TDocument> QueryDescriptor { get; set; }

		private Action<QueryDsl.QueryContainerDescriptor<TDocument>> QueryDescriptorAction { get; set; }

		private Elastic.Clients.Elasticsearch.SlicedScroll? SliceValue { get; set; }

		private SlicedScrollDescriptor<TDocument> SliceDescriptor { get; set; }

		private Action<SlicedScrollDescriptor<TDocument>> SliceDescriptorAction { get; set; }

		private Elastic.Clients.Elasticsearch.Fields? SourceFieldsValue { get; set; }

		private Elastic.Clients.Elasticsearch.Indices IndexValue { get; set; }

		private Elastic.Clients.Elasticsearch.RemoteSource? RemoteValue { get; set; }

		private RemoteSourceDescriptor RemoteDescriptor { get; set; }

		private Action<RemoteSourceDescriptor> RemoteDescriptorAction { get; set; }

		private Dictionary<Elastic.Clients.Elasticsearch.Field, IEnumerable<Elastic.Clients.Elasticsearch.Mapping.RuntimeField>>? RuntimeMappingsValue { get; set; }

		private int? SizeValue { get; set; }

		private IEnumerable<Elastic.Clients.Elasticsearch.SortCombinations>? SortValue { get; set; }

		public SourceDescriptor<TDocument> Query(Elastic.Clients.Elasticsearch.QueryDsl.QueryContainer? query)
		{
			QueryDescriptor = null;
			QueryDescriptorAction = null;
			QueryValue = query;
			return Self;
		}

		public SourceDescriptor<TDocument> Query(QueryDsl.QueryContainerDescriptor<TDocument> descriptor)
		{
			QueryValue = null;
			QueryDescriptorAction = null;
			QueryDescriptor = descriptor;
			return Self;
		}

		public SourceDescriptor<TDocument> Query(Action<QueryDsl.QueryContainerDescriptor<TDocument>> configure)
		{
			QueryValue = null;
			QueryDescriptor = null;
			QueryDescriptorAction = configure;
			return Self;
		}

		public SourceDescriptor<TDocument> Slice(Elastic.Clients.Elasticsearch.SlicedScroll? slice)
		{
			SliceDescriptor = null;
			SliceDescriptorAction = null;
			SliceValue = slice;
			return Self;
		}

		public SourceDescriptor<TDocument> Slice(SlicedScrollDescriptor<TDocument> descriptor)
		{
			SliceValue = null;
			SliceDescriptorAction = null;
			SliceDescriptor = descriptor;
			return Self;
		}

		public SourceDescriptor<TDocument> Slice(Action<SlicedScrollDescriptor<TDocument>> configure)
		{
			SliceValue = null;
			SliceDescriptor = null;
			SliceDescriptorAction = configure;
			return Self;
		}

		public SourceDescriptor<TDocument> SourceFields(Elastic.Clients.Elasticsearch.Fields? sourceFields)
		{
			SourceFieldsValue = sourceFields;
			return Self;
		}

		public SourceDescriptor<TDocument> SourceFields<TValue>(Expression<Func<TDocument, TValue>> sourceFields)
		{
			SourceFieldsValue = sourceFields;
			return Self;
		}

		public SourceDescriptor<TDocument> Index(Elastic.Clients.Elasticsearch.Indices index)
		{
			IndexValue = index;
			return Self;
		}

		public SourceDescriptor<TDocument> Remote(Elastic.Clients.Elasticsearch.RemoteSource? remote)
		{
			RemoteDescriptor = null;
			RemoteDescriptorAction = null;
			RemoteValue = remote;
			return Self;
		}

		public SourceDescriptor<TDocument> Remote(RemoteSourceDescriptor descriptor)
		{
			RemoteValue = null;
			RemoteDescriptorAction = null;
			RemoteDescriptor = descriptor;
			return Self;
		}

		public SourceDescriptor<TDocument> Remote(Action<RemoteSourceDescriptor> configure)
		{
			RemoteValue = null;
			RemoteDescriptor = null;
			RemoteDescriptorAction = configure;
			return Self;
		}

		public SourceDescriptor<TDocument> RuntimeMappings(Func<FluentDictionary<Elastic.Clients.Elasticsearch.Field, IEnumerable<Elastic.Clients.Elasticsearch.Mapping.RuntimeField>>, FluentDictionary<Elastic.Clients.Elasticsearch.Field, IEnumerable<Elastic.Clients.Elasticsearch.Mapping.RuntimeField>>> selector)
		{
			RuntimeMappingsValue = selector?.Invoke(new FluentDictionary<Elastic.Clients.Elasticsearch.Field, IEnumerable<Elastic.Clients.Elasticsearch.Mapping.RuntimeField>>());
			return Self;
		}

		public SourceDescriptor<TDocument> Size(int? size)
		{
			SizeValue = size;
			return Self;
		}

		public SourceDescriptor<TDocument> Sort(IEnumerable<Elastic.Clients.Elasticsearch.SortCombinations>? sort)
		{
			SortValue = sort;
			return Self;
		}

		protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
		{
			writer.WriteStartObject();
			if (QueryDescriptor is not null)
			{
				writer.WritePropertyName("query");
				JsonSerializer.Serialize(writer, QueryDescriptor, options);
			}
			else if (QueryDescriptorAction is not null)
			{
				writer.WritePropertyName("query");
				JsonSerializer.Serialize(writer, new QueryDsl.QueryContainerDescriptor<TDocument>(QueryDescriptorAction), options);
			}
			else if (QueryValue is not null)
			{
				writer.WritePropertyName("query");
				JsonSerializer.Serialize(writer, QueryValue, options);
			}

			if (SliceDescriptor is not null)
			{
				writer.WritePropertyName("slice");
				JsonSerializer.Serialize(writer, SliceDescriptor, options);
			}
			else if (SliceDescriptorAction is not null)
			{
				writer.WritePropertyName("slice");
				JsonSerializer.Serialize(writer, new SlicedScrollDescriptor<TDocument>(SliceDescriptorAction), options);
			}
			else if (SliceValue is not null)
			{
				writer.WritePropertyName("slice");
				JsonSerializer.Serialize(writer, SliceValue, options);
			}

			if (SourceFieldsValue is not null)
			{
				writer.WritePropertyName("_source");
				JsonSerializer.Serialize(writer, SourceFieldsValue, options);
			}

			writer.WritePropertyName("index");
			JsonSerializer.Serialize(writer, IndexValue, options);
			if (RemoteDescriptor is not null)
			{
				writer.WritePropertyName("remote");
				JsonSerializer.Serialize(writer, RemoteDescriptor, options);
			}
			else if (RemoteDescriptorAction is not null)
			{
				writer.WritePropertyName("remote");
				JsonSerializer.Serialize(writer, new RemoteSourceDescriptor(RemoteDescriptorAction), options);
			}
			else if (RemoteValue is not null)
			{
				writer.WritePropertyName("remote");
				JsonSerializer.Serialize(writer, RemoteValue, options);
			}

			if (RuntimeMappingsValue is not null)
			{
				writer.WritePropertyName("runtime_mappings");
				JsonSerializer.Serialize(writer, RuntimeMappingsValue, options);
			}

			if (SizeValue.HasValue)
			{
				writer.WritePropertyName("size");
				writer.WriteNumberValue(SizeValue.Value);
			}

			if (SortValue is not null)
			{
				writer.WritePropertyName("sort");
				SingleOrManySerializationHelper.Serialize<Elastic.Clients.Elasticsearch.SortCombinations>(SortValue, writer, options);
			}

			writer.WriteEndObject();
		}
	}

	public sealed partial class SourceDescriptor : SerializableDescriptorBase<SourceDescriptor>
	{
		internal SourceDescriptor(Action<SourceDescriptor> configure) => configure.Invoke(this);
		public SourceDescriptor() : base()
		{
		}

		private Elastic.Clients.Elasticsearch.QueryDsl.QueryContainer? QueryValue { get; set; }

		private QueryDsl.QueryContainerDescriptor QueryDescriptor { get; set; }

		private Action<QueryDsl.QueryContainerDescriptor> QueryDescriptorAction { get; set; }

		private Elastic.Clients.Elasticsearch.SlicedScroll? SliceValue { get; set; }

		private SlicedScrollDescriptor SliceDescriptor { get; set; }

		private Action<SlicedScrollDescriptor> SliceDescriptorAction { get; set; }

		private Elastic.Clients.Elasticsearch.Fields? SourceFieldsValue { get; set; }

		private Elastic.Clients.Elasticsearch.Indices IndexValue { get; set; }

		private Elastic.Clients.Elasticsearch.RemoteSource? RemoteValue { get; set; }

		private RemoteSourceDescriptor RemoteDescriptor { get; set; }

		private Action<RemoteSourceDescriptor> RemoteDescriptorAction { get; set; }

		private Dictionary<Elastic.Clients.Elasticsearch.Field, IEnumerable<Elastic.Clients.Elasticsearch.Mapping.RuntimeField>>? RuntimeMappingsValue { get; set; }

		private int? SizeValue { get; set; }

		private IEnumerable<Elastic.Clients.Elasticsearch.SortCombinations>? SortValue { get; set; }

		public SourceDescriptor Query(Elastic.Clients.Elasticsearch.QueryDsl.QueryContainer? query)
		{
			QueryDescriptor = null;
			QueryDescriptorAction = null;
			QueryValue = query;
			return Self;
		}

		public SourceDescriptor Query(QueryDsl.QueryContainerDescriptor descriptor)
		{
			QueryValue = null;
			QueryDescriptorAction = null;
			QueryDescriptor = descriptor;
			return Self;
		}

		public SourceDescriptor Query(Action<QueryDsl.QueryContainerDescriptor> configure)
		{
			QueryValue = null;
			QueryDescriptor = null;
			QueryDescriptorAction = configure;
			return Self;
		}

		public SourceDescriptor Slice(Elastic.Clients.Elasticsearch.SlicedScroll? slice)
		{
			SliceDescriptor = null;
			SliceDescriptorAction = null;
			SliceValue = slice;
			return Self;
		}

		public SourceDescriptor Slice(SlicedScrollDescriptor descriptor)
		{
			SliceValue = null;
			SliceDescriptorAction = null;
			SliceDescriptor = descriptor;
			return Self;
		}

		public SourceDescriptor Slice(Action<SlicedScrollDescriptor> configure)
		{
			SliceValue = null;
			SliceDescriptor = null;
			SliceDescriptorAction = configure;
			return Self;
		}

		public SourceDescriptor SourceFields(Elastic.Clients.Elasticsearch.Fields? sourceFields)
		{
			SourceFieldsValue = sourceFields;
			return Self;
		}

		public SourceDescriptor SourceFields<TDocument, TValue>(Expression<Func<TDocument, TValue>> sourceFields)
		{
			SourceFieldsValue = sourceFields;
			return Self;
		}

		public SourceDescriptor SourceFields<TDocument>(Expression<Func<TDocument, object>> sourceFields)
		{
			SourceFieldsValue = sourceFields;
			return Self;
		}

		public SourceDescriptor Index(Elastic.Clients.Elasticsearch.Indices index)
		{
			IndexValue = index;
			return Self;
		}

		public SourceDescriptor Remote(Elastic.Clients.Elasticsearch.RemoteSource? remote)
		{
			RemoteDescriptor = null;
			RemoteDescriptorAction = null;
			RemoteValue = remote;
			return Self;
		}

		public SourceDescriptor Remote(RemoteSourceDescriptor descriptor)
		{
			RemoteValue = null;
			RemoteDescriptorAction = null;
			RemoteDescriptor = descriptor;
			return Self;
		}

		public SourceDescriptor Remote(Action<RemoteSourceDescriptor> configure)
		{
			RemoteValue = null;
			RemoteDescriptor = null;
			RemoteDescriptorAction = configure;
			return Self;
		}

		public SourceDescriptor RuntimeMappings(Func<FluentDictionary<Elastic.Clients.Elasticsearch.Field, IEnumerable<Elastic.Clients.Elasticsearch.Mapping.RuntimeField>>, FluentDictionary<Elastic.Clients.Elasticsearch.Field, IEnumerable<Elastic.Clients.Elasticsearch.Mapping.RuntimeField>>> selector)
		{
			RuntimeMappingsValue = selector?.Invoke(new FluentDictionary<Elastic.Clients.Elasticsearch.Field, IEnumerable<Elastic.Clients.Elasticsearch.Mapping.RuntimeField>>());
			return Self;
		}

		public SourceDescriptor Size(int? size)
		{
			SizeValue = size;
			return Self;
		}

		public SourceDescriptor Sort(IEnumerable<Elastic.Clients.Elasticsearch.SortCombinations>? sort)
		{
			SortValue = sort;
			return Self;
		}

		protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
		{
			writer.WriteStartObject();
			if (QueryDescriptor is not null)
			{
				writer.WritePropertyName("query");
				JsonSerializer.Serialize(writer, QueryDescriptor, options);
			}
			else if (QueryDescriptorAction is not null)
			{
				writer.WritePropertyName("query");
				JsonSerializer.Serialize(writer, new QueryDsl.QueryContainerDescriptor(QueryDescriptorAction), options);
			}
			else if (QueryValue is not null)
			{
				writer.WritePropertyName("query");
				JsonSerializer.Serialize(writer, QueryValue, options);
			}

			if (SliceDescriptor is not null)
			{
				writer.WritePropertyName("slice");
				JsonSerializer.Serialize(writer, SliceDescriptor, options);
			}
			else if (SliceDescriptorAction is not null)
			{
				writer.WritePropertyName("slice");
				JsonSerializer.Serialize(writer, new SlicedScrollDescriptor(SliceDescriptorAction), options);
			}
			else if (SliceValue is not null)
			{
				writer.WritePropertyName("slice");
				JsonSerializer.Serialize(writer, SliceValue, options);
			}

			if (SourceFieldsValue is not null)
			{
				writer.WritePropertyName("_source");
				JsonSerializer.Serialize(writer, SourceFieldsValue, options);
			}

			writer.WritePropertyName("index");
			JsonSerializer.Serialize(writer, IndexValue, options);
			if (RemoteDescriptor is not null)
			{
				writer.WritePropertyName("remote");
				JsonSerializer.Serialize(writer, RemoteDescriptor, options);
			}
			else if (RemoteDescriptorAction is not null)
			{
				writer.WritePropertyName("remote");
				JsonSerializer.Serialize(writer, new RemoteSourceDescriptor(RemoteDescriptorAction), options);
			}
			else if (RemoteValue is not null)
			{
				writer.WritePropertyName("remote");
				JsonSerializer.Serialize(writer, RemoteValue, options);
			}

			if (RuntimeMappingsValue is not null)
			{
				writer.WritePropertyName("runtime_mappings");
				JsonSerializer.Serialize(writer, RuntimeMappingsValue, options);
			}

			if (SizeValue.HasValue)
			{
				writer.WritePropertyName("size");
				writer.WriteNumberValue(SizeValue.Value);
			}

			if (SortValue is not null)
			{
				writer.WritePropertyName("sort");
				SingleOrManySerializationHelper.Serialize<Elastic.Clients.Elasticsearch.SortCombinations>(SortValue, writer, options);
			}

			writer.WriteEndObject();
		}
	}
}